var mysql = require('mysql');

const pool = mysql.createPool({
	host: 'localhost',
	user: 'root',
	password: '',
	port: 3306,
	database: 'fber',
	timezone: '08:00'
})

function doMysql(sql, params = []) {
	return new Promise((resolve, reject) => {
		pool.getConnection((err, connection) => {
			if (err) throw err
			connection.query(sql, params, (err, result) => {
				if (err) {
					return reject(err);
				}
				resolve(result);
				connection.release();
			})
		})
	})
};

function backText(data, total) {
	return {
		data: data,
		success: true,
		message: "数据请求成功",
		errorCode: 0,
		total: total,
		code: 20000
	}
}

function postText(data) {
	return {
		data: data,
		success: true,
		message: "数据增加成功",
		errorCode: 0,
		code: 20000,
	}
}

function delText(data) {
	return {
		data: data,
		success: true,
		message: "数据删除成功",
		errorCode: 0,
		code: 20000,
	}
}

function handerYes(data) {
	return {
		success: 'true',
		errcode: 0,
		code: 20000,
		msg: '数据请求成功',
		data: data,
	}
}

function fool() {
	return {
		'success': 'true',
		'errorCode': 401,
		'msg': '当前没有数据',
	}
}

//失败
var errFn = (err) => {
	return {
		success: false,
		errcode: 0,
		msg: '数据请求失败',
		data: err,
		code: 0000
	}
}

exports.doMysql = doMysql
exports.backText = backText
exports.postText = postText
exports.delText = delText
exports.handerYes = handerYes
exports.fool = fool
exports.errFn = errFn